package com.prs.crm.service.fs.impl;

import java.io.File;

import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.dao.fs.TransferAccountDao;
import com.prs.crm.domain.Attachment;
import com.prs.crm.domain.fs.Detail;
import com.prs.crm.domain.fs.TransferAccount;
import com.prs.crm.service.fs.DetailService;
import com.prs.crm.service.fs.TransferAccountService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class TransferAccountServiceImpl extends BaseServiceImpl<TransferAccount,TransferAccountDao> implements TransferAccountService{
	private DetailService detailService;
	
	@Override
	public void submitTransfer(TransferAccount transfer, File file, String fileName) {
		// TODO Auto-generated method stub
		Attachment attach = FileUtil.getInstance().createAttach(file, fileName);
		if(attach != null) {
			transfer.setAttach(attach);
		}
		transfer.setState(Constrains.TASK_AUDITING);
		this.getDao().save(transfer);
		
		Double srcBalance = this.getDetailService().getBalanceByAccount(transfer.getSrcAccount());
		Double aimBalance = this.getDetailService().getBalanceByAccount(transfer.getAimAccount());
		
		Detail srcDetail = new Detail();
		srcDetail.setBankAccount(transfer.getSrcAccount());
		srcDetail.setIncome(0.00);
		srcDetail.setPay(transfer.getMoney());
		srcDetail.setTransfer(transfer);
		srcDetail.setItem(Constrains.FS_ITEMS_TRANSFER);
		srcDetail.setBalance(srcBalance - transfer.getMoney());
		this.getDetailService().save(srcDetail);
		
		
		Detail aimDetail = new Detail();
		aimDetail.setBankAccount(transfer.getAimAccount());
		aimDetail.setIncome(transfer.getMoney());
		aimDetail.setPay(0.00);
		aimDetail.setTransfer(transfer);
		aimDetail.setItem(Constrains.FS_ITEMS_TRANSFER);
		aimDetail.setBalance(aimBalance + transfer.getMoney());
		this.getDetailService().save(aimDetail);
	}

	public void setDetailService(DetailService detailService) {
		this.detailService = detailService;
	}

	public DetailService getDetailService() {
		return detailService;
	}

}
